<%@page import="java.util.Random"%>
<%@page import="java.awt.Font"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.Image"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Graphics2D"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	int width = 400;
    int height = 200;
    //声明图片流文件
    BufferedImage buffImg = 
    	new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    
    //声明2d图片对象
    Graphics2D g = buffImg.createGraphics();
    
    //画实心的矩形
    g.setColor(new Color(255,158,0));
    g.fillRect(0, 0, width, height);
    
    String code="abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ23456789";
    
    //设置字体
    Font f = new Font("Times New Roman",Font.BOLD,80);
    g.setFont(f);
    g.setColor(Color.WHITE);
    StringBuffer sb = new StringBuffer(); 
    Random r = new Random();
    for(int i=0;i<4;i++){
    	int index = r.nextInt(code.length());
    	sb.append(code.charAt(index));
    }
    
    //把验证码存入会话
    session.setAttribute("code", sb.toString());
    
    
    g.drawString(sb.toString(), 100, 120);
    
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");
    
    ServletOutputStream sos = response.getOutputStream();
    ImageIO.write(buffImg, "jpeg", sos);
    
    sos.close();
    out.clear();
    out = pageContext.pushBody();
    
    
    
    
    
    
    
    
%>